package patrol.mobile.com.mobilepatrol.activity.security.emergency;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.activity.security.ConsTitles;
import patrol.mobile.com.mobilepatrol.activity.security.StickyEventBody;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencyPeopleInfoFamilyAdapter;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencyPeopleInfoFamilyFlowAdapter;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencyPeopleInfoHouseToAdapter;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencyPeopleInfoLendAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.PeopleInfoBean;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.widget.MyImageView;

/**
 * Created by K on 2018/9/14.
 */
public class EmergencyPeopleInfoActivity extends BaseActivity {


    @BindView(R.id.tv_perName)
    TextView tvPerName;
    @BindView(R.id.tv_perSex)
    TextView tvPerSex;
    @BindView(R.id.tv_idCard)
    TextView tvIdCard;
    @BindView(R.id.tv_national)
    TextView tvNational;
    @BindView(R.id.tv_perType)
    TextView tvPerType;
    @BindView(R.id.tv_censusRegister)
    TextView tvCensusRegister;
    @BindView(R.id.tv_position)
    TextView tvPosition;
    @BindView(R.id.tv_company)
    TextView tvCompany;
    @BindView(R.id.tv_education)
    TextView tvEducation;
    @BindView(R.id.tv_political)
    TextView tvPolitical;
    @BindView(R.id.tv_familySize)
    TextView tvFamilySize;
    @BindView(R.id.tv_familyType)
    TextView tvFamilyType;
    @BindView(R.id.tv_familyAddress)
    TextView tvFamilyAddress;
    @BindView(R.id.tv_perMobile)
    TextView tvPerMobile;
    @BindView(R.id.tv_impSpec)
    TextView tvImpSpec;
    @BindView(R.id.tv_belief)
    TextView tvBelief;
    @BindView(R.id.tv_impSpecImfo)
    TextView tvImpSpecImfo;
    @BindView(R.id.tv_houseRented)
    TextView tvHouseRented;
    @BindView(R.id.tv_hrNo)
    TextView tvHrNo;
    @BindView(R.id.iv_people)
    MyImageView ivPeople;
    @BindView(R.id.tv_historyRecorder)
    TextView tvhistoryRecorder;
    @BindView(R.id.tv_visitRecorder)
    TextView tvVisitRecorder;
    @BindView(R.id.iv_arrowLeft)
    ImageView ivArrowLeft;
    @BindView(R.id.iv_arrowRight)
    ImageView ivArrowRight;
    @BindView(R.id.sc_info)
    ScrollView scrollView;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;


    private String id = "";
    private String holderId = "";
    private String from = "";
    private NUM currentNum = NUM.none;

    private String perName = "";
    private List<PeopleInfoBean.AnbkFamilyInformationListBean> anbkFamilyInformationList = new ArrayList<>();
    private List<PeopleInfoBean.AnbkMobilepupInformationListBean> anbkMobilepupInformationListBean = new ArrayList<>();
    private List<PeopleInfoBean.AnbkRentedHouseListBean> anbkRentedHouseListBean = new ArrayList<>();
    private List<PeopleInfoBean.AnbkHouseToRentListBean> anbkHouseToRentListBeans = new ArrayList<>();


    private EmergencyPeopleInfoFamilyAdapter emergencyPeopleInfoFamilyAdapter;
    private EmergencyPeopleInfoFamilyFlowAdapter emergencyPeopleInfoFamilyFlowAdapter;
    private EmergencyPeopleInfoLendAdapter emergencyPeopleInfoLendAdapter;
    private EmergencyPeopleInfoHouseToAdapter emergencyPeopleInfoHouseToAdapter;


    enum NUM {
        none,
        one,
        two,
        three,
        four
    }

    @BindView(R.id.vp_other_business)
    ViewPager vpOtherBusiness;

    private int indx = 0;

    private Fragment[] tabs;

    @Override
    public void initData() {
        super.initData();
        TabFragmentAdapter adapter = new TabFragmentAdapter(getSupportFragmentManager(), this);
        vpOtherBusiness.setAdapter(adapter);
        vpOtherBusiness.setOffscreenPageLimit(5);
        switchTab(indx);

        vpOtherBusiness.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                indx = position;

                if (position == 0) {
                    currentNum = NUM.none;
                    ivArrowLeft.setVisibility(View.GONE);
                    ivArrowRight.setVisibility(View.VISIBLE);
                    setMainTitle(ConsTitles.EMERGENCY_PEOPLE_INFO);
                } else if (position == 1) {
                    currentNum = NUM.one;
                    ivArrowLeft.setVisibility(View.VISIBLE);
                    ivArrowRight.setVisibility(View.VISIBLE);
                    setMainTitle(ConsTitles.EMERGENCY_PROTECT_FAMILY);
                } else if (position == 2) {
                    currentNum = NUM.two;
                    ivArrowLeft.setVisibility(View.VISIBLE);
                    ivArrowRight.setVisibility(View.VISIBLE);
                    setMainTitle(ConsTitles.EMERGENCY_PROTECT_FAMILY_FLOW);
                } else if (position == 3) {
                    currentNum = NUM.three;
                    ivArrowLeft.setVisibility(View.VISIBLE);
                    ivArrowRight.setVisibility(View.VISIBLE);
                    setMainTitle(ConsTitles.EMERGENCY_PROTECT_LEND_HOUSE);
                } else if (position == 4) {
                    currentNum = NUM.four;
                    ivArrowLeft.setVisibility(View.VISIBLE);
                    ivArrowRight.setVisibility(View.GONE);
                    setMainTitle(ConsTitles.EMERGENCY_PROTECT_REND_HOUSE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }

    private void switchTab(int index) {
        if (vpOtherBusiness != null) {
            vpOtherBusiness.setCurrentItem(index, false);
        }
    }


    /*
     * 此结构为activity+fragment+fragment
     */
    class TabFragmentAdapter extends FragmentPagerAdapter {
        private String[] TITLES;


        private TabFragmentAdapter(FragmentManager fm, Activity context) {
            super(fm);
            TITLES = new String[5];
            tabs = new Fragment[TITLES.length];

            tabs[0] = new EmergencyPeopleInfoBodyFragment();
            tabs[1] = new EmergencyPeopleInfoFamilyFragment();
            tabs[2] = new EmergencyPeopleInfoFamilyFlowFragment();
            tabs[3] = new EmergencyPeopleInfoLendFragment();
            tabs[4] = new EmergencyPeopleInfoHouseFragment();

        }

        @Override
        public Fragment getItem(int position) {
            return tabs[position];
        }

        @Override
        public int getCount() {
            return TITLES.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }
    }


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_emergency_people_info;
    }

    @Override
    public void setListener() {

        Intent intent = getIntent();
        if (null != intent) {
            id = intent.getStringExtra("id");
            holderId = intent.getStringExtra("holderId");
            from = intent.getStringExtra("from");
            if (!TextUtils.isEmpty(from)) {
                tvVisitRecorder.setVisibility(View.GONE);
            }
            requestData(holderId);
        }

//        ivArrowLeft.setAlpha(0.5f);
//        ivArrowRight.setAlpha(0.5f);


        emergencyPeopleInfoFamilyAdapter = new EmergencyPeopleInfoFamilyAdapter(this);
        emergencyPeopleInfoFamilyFlowAdapter = new EmergencyPeopleInfoFamilyFlowAdapter(this);
        emergencyPeopleInfoLendAdapter = new EmergencyPeopleInfoLendAdapter(this);
        emergencyPeopleInfoHouseToAdapter = new EmergencyPeopleInfoHouseToAdapter(this);
        ivArrowLeft.setVisibility(View.GONE);
        ivArrowLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (currentNum == NUM.four) {
                    ivArrowRight.setVisibility(View.VISIBLE);
                    ivArrowLeft.setVisibility(View.VISIBLE);

                    vpOtherBusiness.setCurrentItem(3);
//                    RecyclerViewHelper.initRecyclerViewV(EmergencyPeopleInfoActivity.this,
//                            recyclerView, emergencyPeopleInfoLendAdapter);
//                    emergencyPeopleInfoLendAdapter.updateItems(anbkRentedHouseListBean);
//
//                    tv_center_title.setText(ConsTitles.EMERGENCY_PROTECT_LEND_HOUSE);


                    currentNum = NUM.three;
                } else if (currentNum == NUM.three) {
                    ivArrowLeft.setVisibility(View.VISIBLE);
                    ivArrowRight.setVisibility(View.VISIBLE);

//                    RecyclerViewHelper.initRecyclerViewV(EmergencyPeopleInfoActivity.this,
//                            recyclerView, emergencyPeopleInfoFamilyFlowAdapter);
//                    emergencyPeopleInfoFamilyFlowAdapter.updateItems(anbkMobilepupInformationListBean);
//
//                    tv_center_title.setText(ConsTitles.EMERGENCY_PROTECT_FAMILY_FLOW);
                    vpOtherBusiness.setCurrentItem(2);

                    currentNum = NUM.two;
                } else if (currentNum == NUM.two) {
                    ivArrowLeft.setVisibility(View.VISIBLE);
                    ivArrowRight.setVisibility(View.VISIBLE);

//                    RecyclerViewHelper.initRecyclerViewV(EmergencyPeopleInfoActivity.this,
//                            recyclerView, emergencyPeopleInfoFamilyAdapter);
//                    emergencyPeopleInfoFamilyAdapter.updateItems(anbkFamilyInformationList);
//
//                    tv_center_title.setText(ConsTitles.EMERGENCY_PROTECT_FAMILY);
                    vpOtherBusiness.setCurrentItem(1);

                    currentNum = NUM.one;
                } else if (currentNum == NUM.one) {
                    ivArrowLeft.setVisibility(View.GONE);
                    ivArrowRight.setVisibility(View.VISIBLE);

                    recyclerView.setVisibility(View.GONE);

                    scrollView.setVisibility(View.VISIBLE);

                    // tv_center_title.setText(ConsTitles.EMERGENCY_PEOPLE_INFO);

                    currentNum = NUM.none;
                }
            }
        });


        ivArrowRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (currentNum == NUM.none) {
                    scrollView.setVisibility(View.GONE);
                    ivArrowLeft.setVisibility(View.VISIBLE);
                    ivArrowRight.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.VISIBLE);

//                    RecyclerViewHelper.initRecyclerViewV(EmergencyPeopleInfoActivity.this,
//                            recyclerView, emergencyPeopleInfoFamilyAdapter);
//                    emergencyPeopleInfoFamilyAdapter.updateItems(anbkFamilyInformationList);
//
//                    tv_center_title.setText(ConsTitles.EMERGENCY_PROTECT_FAMILY);
                    vpOtherBusiness.setCurrentItem(1);

                    currentNum = NUM.one;
                } else if (currentNum == NUM.one) {
                    scrollView.setVisibility(View.GONE);
                    ivArrowLeft.setVisibility(View.VISIBLE);
                    ivArrowRight.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.VISIBLE);

                    vpOtherBusiness.setCurrentItem(2);

//                    RecyclerViewHelper.initRecyclerViewV(EmergencyPeopleInfoActivity.this,
//                            recyclerView, emergencyPeopleInfoFamilyFlowAdapter);
//                    emergencyPeopleInfoFamilyFlowAdapter.updateItems(anbkMobilepupInformationListBean);
//                    tv_center_title.setText(ConsTitles.EMERGENCY_PROTECT_FAMILY_FLOW);
                    currentNum = NUM.two;

                } else if (currentNum == NUM.two) {
                    ivArrowLeft.setVisibility(View.VISIBLE);
                    ivArrowRight.setVisibility(View.VISIBLE);

                    vpOtherBusiness.setCurrentItem(3);
//                    tv_center_title.setText(ConsTitles.EMERGENCY_PROTECT_LEND_HOUSE);

                    currentNum = NUM.three;
                } else if (currentNum == NUM.three) {
                    ivArrowLeft.setVisibility(View.VISIBLE);
                    ivArrowRight.setVisibility(View.GONE);

//                    RecyclerViewHelper.initRecyclerViewV(EmergencyPeopleInfoActivity.this,
//                            recyclerView, emergencyPeopleInfoHouseToAdapter);
//                    emergencyPeopleInfoHouseToAdapter.updateItems(anbkHouseToRentListBeans);
//
//                    tv_center_title.setText(ConsTitles.EMERGENCY_PROTECT_REND_HOUSE);
                    vpOtherBusiness.setCurrentItem(4);

                    currentNum = NUM.four;
                }
//                else if (currentNum == NUM.four) {
//                    ivArrowLeft.setVisibility(View.VISIBLE);
//                    ivArrowRight.setVisibility(View.GONE);
//
//
//
//                }
            }
        });


    }

    @Override
    public void initTitle() {
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(EmergencyPeopleInfoActivity.this, true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        //tv_center_title.setText(ConsTitles.EMERGENCY_PEOPLE_INFO);
    }

    @OnClick({R.id.tv_historyRecorder, R.id.tv_visitRecorder})
    public void onhisVisClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {

            case R.id.tv_historyRecorder:
                intent.setClass(this, EmergencyPackageEdit1Activity.class);
                intent.putExtra("holderId", holderId);
                intent.putExtra("perName", perName);
                startActivity(intent);
                break;

            case R.id.tv_visitRecorder:
                intent.setClass(this, EmergencyPackageEdit3Activity.class);
                intent.putExtra("id", id);
                startActivity(intent);
                break;
        }

    }


    public void setMainTitle(String title) {
        tv_center_title.setText(title);
    }

    private PeopleInfoBean protectListBean;

    private void requestData(String id) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.IEMERGENCY_PEOPLE_INFO);
        HashMap<String, String> map = new HashMap<>();
        map.put("id", id);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                protectListBean = GsonUtil.GsonToBean(response, PeopleInfoBean.class);
                EventBus.getDefault().postSticky(new StickyEventBody(protectListBean));

                perName = protectListBean.getPerName();
                tvPerName.setText(protectListBean.getPerName());
                tvPerSex.setText(protectListBean.getPerSex().equals("1") ? "男" : "女");
                tvIdCard.setText(protectListBean.getIdCard());
                tvNational.setText(protectListBean.getNational());
                tvPerType.setText(protectListBean.getPerType());
                tvCensusRegister.setText(protectListBean.getCensusRegister());
                tvPosition.setText(protectListBean.getPosition());
                tvCompany.setText(protectListBean.getCompany());
                tvEducation.setText(protectListBean.getEducation());
                tvPolitical.setText(protectListBean.getPolitical());
                tvFamilySize.setText(protectListBean.getFamilySize());
                tvFamilyType.setText(protectListBean.getFamilyType());
                tvFamilyAddress.setText(protectListBean.getFamilyAddress());
                tvPerMobile.setText(protectListBean.getPerMobile());
                tvImpSpec.setText(protectListBean.getImpSpec());
                tvBelief.setText(protectListBean.getBelief());
                tvImpSpecImfo.setText(protectListBean.getImpSpecImfo());
                tvHouseRented.setText(protectListBean.getHouseRented());
                tvHrNo.setText(protectListBean.getHrNo());
                Glide.
                        with(EmergencyPeopleInfoActivity.this)
                        .load(baseUrl + protectListBean.getCoverImg())//.substring(2)
                        .into(ivPeople);


                anbkFamilyInformationList = protectListBean.getAnbkFamilyInformationList();

                anbkMobilepupInformationListBean = protectListBean.getAnbkMobilepupInformationList();

                anbkRentedHouseListBean = protectListBean.getAnbkRentedHouseList();

                anbkHouseToRentListBeans = protectListBean.getAnbkHouseToRentList();

            }
        });
    }


}
